package 线程和TCP网络编程.TAKS2.Socket;

import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * 用户输入内容用线程创建
 *
 * @author 君未洋
 */
public class SocketInputThread implements Runnable {

    private Socket socket;
    private String name;

    public SocketInputThread(Socket socket, String name) {
        //用初始化的方法把对应客户端的socket类传进来
        this.socket = socket;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            Scanner sc = new Scanner(System.in);
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            //创建一个接收键盘输入的Scanner和一个输出到服务器的打印流对象
            /*疑惑:这里为什么不能用用OutputStream对象*/
            while (true) {
                String str = sc.next();
                pw.println(name + " : " + str);
                pw.flush();
                //这里就单纯将用户输入的名字和内容拼接一下发送到服务器就可以了
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}